package ar.edu.unlp.info.cdiv.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import ar.edu.unlp.info.cdiv.model.Persona;
import ar.edu.unlp.info.cdiv.service.PersonService;

@Component
public class PersonValidator implements Validator{

	@Autowired
	PersonService service;
	
	@Override
	public boolean supports(Class<?> clazz) {
		return clazz.equals(Persona.class);
	}

	@Override
	public void validate(Object object, Errors errors) {
		Persona person = (Persona) object;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.field.required", "Required field");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "error.field.required", "Required field");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user.username", "error.field.required", "Required field");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user.password", "error.field.required", "Required field");
		
//		Person exist = service.findByUsername(person.getUser().getUsername());
		Persona exist = null;
		if (exist != null){
			errors.rejectValue("user.username", "error.field.duplicated","El usuario ya existe");
		}
	}

}
